package com.servers.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.stat.TableStat.Condition;
import com.dao.CakeDao;
import com.entity.Cake;
import com.entity.CakeType;
import com.entity.ConditionCakeType;
import com.servers.CakeServers;

@Service("cakeServers")
public class CakeServersImpl implements CakeServers {
	@Autowired
	private CakeDao cakeDao1;

	public List<Cake> getAllCakes() {

		return cakeDao1.QueryAllOfCake();
	}

	@Override
	public List<Cake> getCakesByKind(int typeId) {
		// TODO Auto-generated method stub
		return cakeDao1.QueryCakeFromCakeID(typeId);
	}

	@Override
	public Cake getCakeByName(String cakeName) {
		//System.out.println("cakeName:"+cakeName);
		return cakeDao1.QueryCakeByName(cakeName);
	}
	@Override
	public Cake getCakeByCakeId(int cid) {
		// TODO Auto-generated method stub
		return cakeDao1.QuseryCakeInfoByCakeId(cid);
	}

	public void setCakeDao(CakeDao cakeDao) {
		this.cakeDao1 = cakeDao;
	}

	@Override
	public int addCake(Cake cake) {
		// TODO Auto-generated method stub
		//System.out.println(cake.getCakeName());
		return cakeDao1.AddCake(cake);
	}

	@Override
	public int addCakeType(CakeType cakeType) {
		// TODO Auto-generated method stub
		//System.out.println(cakeType.getCakeTypeName());
		return cakeDao1.AddCakeType(cakeType);
	}

	@Override
	public List<Cake> getCakeByTypeId(ConditionCakeType conditionCakeType) {
		// TODO Auto-generated method stub
		return cakeDao1.QueryCakeByTypeId(conditionCakeType);
	}
	/**
	 * 根据 条件参数 返回特定的参数
	 */
	@Override
	public Map<String, Object> getIndexJson(String key ) {
		ConditionCakeType conditionCakeType;
		Map<String, Object> map=new HashMap<>();		
		Map<String, Object> map1=new HashMap<>();		
		Map<String, Object> map2=new HashMap<>();		
		Map<String, Object> map3=new HashMap<>();
		Map<String, Object> map4=new HashMap<>();		
		Map<String, Object> map5=new HashMap<>();
		Map<String, Object> map6=new HashMap<>();
		List<Map<String, Object>> list1=new ArrayList<>();
		List<Map<String, Object>> list2=new ArrayList<>();
		List<Map<String, Object>> list3=new ArrayList<>();
		
		if (key.trim().equals("floor")) {
			
			// 封装一个特定的 bigimg 小表头
			map1.put("bigimg", "res/static/img/s_img1.jpg");
			
			//封装 最内层 count 的 List 内的 map
			conditionCakeType=new ConditionCakeType(8, "cakesales", "asc");
			List<Cake> cList=cakeDao1.QueryCakeByTypeId(conditionCakeType);
			for (int i = 0; i < 8; i++) {
				if (i<=3) {
					map3=new HashMap<>();
					map3.put("img", "images/Fruits/"+cList.get(i).getCakeName()+".png");
					//System.out.println("aaaaaaaaaaaaaaaaa:"+"images/Fruits/"+cList.get(i).getCakeName()+".png");
					map3.put("title", cList.get(i).getCakeName());
					map3.put("price", cList.get(i).getCakePrice());// 打过折的 现价
					map3.put("original", cList.get(i).getCakePrice()+50);// 灰色的 原价 无情的加上50
					list2.add(map3);
				}else if (i>3) {
					map4=new HashMap<>();
					map4.put("img", "images/Fruits/"+cList.get(i).getCakeName()+".png");
					//System.out.println("bbbbbbbbbbbbbbbbbbb:"+"images/Fruits/"+cList.get(i).getCakeName()+".png");
					map4.put("title", cList.get(i).getCakeName());
					map4.put("price", cList.get(i).getCakePrice());// 打过折的 现价
					map4.put("original", cList.get(i).getCakePrice()+50);// 灰色的 原价 无情的加上50
					list3.add(map4);
				}
			}
			map5.put("count", list2);
			map6.put("count", list3);
			list1.add(map5);
			list1.add(map6);
			map1.put("banner", list1);
			
			map.put(key, map1);
			
			return map;
		}else if (key.trim().equals("hotCon")) {
			//封装 最内层 samllImg 的 List 内的 map
			conditionCakeType=new ConditionCakeType(8, "cakesales", "desc");
			List<Cake> cList=cakeDao1.QueryCakeByTypeId(conditionCakeType);
			for (int i = 0; i < 7; i++) {
				map1=new HashMap<>();
					map1.put("img", "images/Fruits/"+cList.get(i).getCakeName()+".png");
					//map1.put("title", cList.get(i).getCakeName());
					//map1.put("price", cList.get(i).getCakePrice());// 打过折的 现价
					//map1.put("original", cList.get(i).getCakePrice()+50);// 灰色的 原价 无情的加上50
					list1.add(map1);
			}
			map2.put("title","热销推荐");
			map2.put("bigImg","res/static/img/hot1.png");
			map2.put("smallImg",list1);
			map.put(key, map2);
			return map;
		}else if (key.trim().equals("proct")) {
			ConditionCakeType	conditionCakeType1=new ConditionCakeType(1, "cakesales", "desc");
			Map<String, Object> proctMap1 = getProctMap(conditionCakeType1,"蛋糕精品");
			ConditionCakeType	conditionCakeType2=new ConditionCakeType(6, "cakesales", "desc");
			Map<String, Object> proctMap2 = getProctMap(conditionCakeType2,"吐司美味");
			ConditionCakeType	conditionCakeType3=new ConditionCakeType(7, "cakesales", "desc");
			Map<String, Object> proctMap3 = getProctMap(conditionCakeType3,"精美礼品");
			list1.add(proctMap1);
			list1.add(proctMap2);
			list1.add(proctMap3);
			
			map.put(key, list1);
			return map;
		}else if (key.trim().equals("productList")) {
			ConditionCakeType	conditionCakeType1=new ConditionCakeType(8, "cakesales","asc");
			Map<String, Object> productListMap = getProductListMap(conditionCakeType1);
			
			map.put(key, productListMap);
			return map;
		}
		
		return null;
	}

	
	public Map<String, Object> getProctMap(ConditionCakeType conditionCakeType,String text){
		List<Cake> cList = cakeDao1.QueryCakeByTypeId(conditionCakeType);
		List<Map<String, Object>> list=new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			Map<String, Object> map1=new HashMap<>();
			map1.put("img", "images/Fruits/"+cList.get(i).getCakeName()+".png");
			map1.put("text", cList.get(i).getCakeName());
			list.add(map1);
		}
		Map<String, Object> map2=new HashMap<>();
		map2.put("carImg","res/static/img/icon_gou.png");
		map2.put("text",text);
		map2.put("bigImg","res/static/img/img12.jpg");
		map2.put("imgBox",list);
		return map2;
	}
	public Map<String, Object> getProductListMap(ConditionCakeType conditionCakeType){
		//条件查询, 吧今日推销 除外的 其他所有 产品  先遍历 20个 
		List<Cake> cList = cakeDao1.QueryCakeByTypeId(conditionCakeType);
		List<Map<String, Object>> list=new ArrayList<>();
		for (int i = 0; i < 30; i++) {
			if (i>=8) {
				Map<String, Object> map1=new HashMap<>();
				map1.put("img", "images/Fruits/"+cList.get(i).getCakeName()+".png");
				map1.put("text", cList.get(i).getCakeName());
				map1.put("price", "¥"+String.valueOf(cList.get(i).getCakePrice()));
				list.add(map1);
			}
		}
		Map<String, Object> map2=new HashMap<>();
		map2.put("title","更多推荐");
		map2.put("item",list);
		
		return map2;
	}

	@Override
	public Map<String, Object> getCommodityMilkJson(ConditionCakeType conditionCakeType) {
		List<Cake> cakeByTypeId = getCakeByTypeId(conditionCakeType);
		List<Map<String, Object>> list=new ArrayList<>();
		for (Cake cake : cakeByTypeId) {
			Map<String, Object> tempmap=new HashMap<>();
			tempmap.put("img", "images/Fruits/"+cake.getCakeName()+".png");
			tempmap.put("title", cake.getCakeName());
			tempmap.put("pri", cake.getCakePrice());
			tempmap.put("nub", cake.getCakeSales()+"付款");
			list.add(tempmap);
		}
		Map<String, Object> map=new HashMap<>();
		map.put("content", list);
		Map<String, Object> map2=new HashMap<>();
		map2.put("milk", map);
		return map2;
	}
/**
 * 12/15  吐司 和 礼品 未实现
 */
	@Override
	public Map<String, Object> getgoBuytodayJson(ConditionCakeType conditionCakeType,String typeList,String tuorli) {
		if (typeList.trim().equals("productList")) {
			Map<String, Object> buytodayProductListJson = getBuytodayProductListJson(conditionCakeType,tuorli);
			return buytodayProductListJson;
		}else if (typeList.trim().equals("footerList")) {
			Map<String, Object> buytodayFooterListJson = getBuytodayFooterListJson(conditionCakeType);
			return buytodayFooterListJson;
		}else if (typeList.trim().equals("searchList")) {
		Map<String, Object> buytodayFooterListJson = getBuytodayProductListJson(conditionCakeType,tuorli);
		return buytodayFooterListJson;
	}
		
		return null;
	}
	/**
	 * 封装 吐司 跟 礼品
	 * @param conditionCakeType
	 * @return
	 */
	public Map<String, Object> getBuytodayProductListJson(ConditionCakeType conditionCakeType,String tuorli){
		List<Cake> queryCakeByTypeId = cakeDao1.QueryCakeByTypeId(conditionCakeType);
		List<Map<String, Object>> list=new ArrayList<>();
		for (Cake cake : queryCakeByTypeId) {
			Map<String, Object> map=new HashMap<>();
			map.put("img","images/Fruits/"+cake.getCakeName()+".png");// 图片路径
			map.put("title",cake.getCakeName() );//图片标题. name
			map.put("ciurPic",cake.getCakePrice());//现价
			map.put("OriPic",cake.getCakePrice()*2 );//原价
			map.put("discount", "5折");//打折 折扣
			list.add(map);
		}
		Map<String, Object> map1=new HashMap<>();
		if (tuorli.trim().equals("tusi")) {
			map1.put("productTusiList", list);
		}else if (tuorli.trim().equals("lipin")) {
			map1.put("productLipinList", list);
		}else if (tuorli.trim().equals("search")) {
			map1.put("searchlist", list);
		}{
			
		}
		return map1;
	}
	public Map<String, Object> getBuytodayFooterListJson(ConditionCakeType conditionCakeType){
		List<Cake> queryCakeByTypeId = cakeDao1.QueryCakeByTypeId(conditionCakeType);
		List<Map<String, Object>> list=new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			
			Map<String, Object> map=new HashMap<>();
			map.put("img","images/Fruits/"+queryCakeByTypeId.get(i).getCakeName()+".png");// 图片路径
			map.put("title",queryCakeByTypeId.get(i).getCakeName() );//图片标题. name
			map.put("price","¥"+queryCakeByTypeId.get(i).getCakePrice());//现价
			list.add(map);
		}
		Map<String, Object> map1=new HashMap<>();
		map1.put("footerList", list);
		return map1;
	}

	@Override
	public List<Cake> QuerySearchAllCake(String title) {
		// TODO Auto-generated method stub
		return cakeDao1.QuerySearchAllCake(title);
	}

	@Override
	public List<Cake> findCakesLimit(int a, int b) {
		// TODO Auto-generated method stub
		return cakeDao1.findCakesLimit(a, b);
	}
}

